package com.aios.action;

import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;

import com.aios.entity.SystemUser;
import com.aios.exception.SessionNotExist;
import com.aios.util.DataManipulator;
import com.aios.util.SessionKey;
import com.opensymphony.xwork2.ActionSupport;

public abstract class AbstractAction extends ActionSupport implements
		SessionAware {

	private static final long serialVersionUID = 1L;
	public static final Integer DEFAULT_HEADER_KEY = -1;
	public static final String DEFAULT_HEADER_TEXT = "Invalid";

	public enum STATUS {
		SUCCESS, FAILURE, NOT_FOUND, SERVER_ERROR, DB_ERROR
	}

	private static Logger logger = Logger.getLogger(AbstractAction.class);
	private static final String SESSION_NOT_EXIST = "Session doesn't exist";
	private Map<SessionKey, Object> session = null;

	private int x;
	private int y;

	protected STATUS status;

	public String alwaysSuccess() {
		return SUCCESS;
	}

	public void addSessionUserVariable(SystemUser user) throws SessionNotExist {
		if (session != null) {
			session.put(SessionKey.SESSION_USER, user);
		} else {
			logger.error(SESSION_NOT_EXIST);
			throw new SessionNotExist(SESSION_NOT_EXIST);
		}
	}

	public SystemUser getSessionUserVariable() throws SessionNotExist {
		if (session != null) {
			if (session.containsKey(SessionKey.SESSION_USER)) {
				return (SystemUser) session.get(SessionKey.SESSION_USER);
			} else {
				throw new SessionNotExist(SESSION_NOT_EXIST);
			}
		} else {
			logger.error(SESSION_NOT_EXIST);
			throw new SessionNotExist(SESSION_NOT_EXIST);
		}
	}

	public void removeSessionVariable(SessionKey key) throws SessionNotExist {
		if (session != null) {
			if (session.containsKey(key)) {
				session.remove(key);
			}
		} else {
			logger.error(SESSION_NOT_EXIST);
			throw new SessionNotExist(SESSION_NOT_EXIST);
		}
	}

	public boolean verifySessionUser() throws SessionNotExist {
		if (session != null) {
			return session.containsKey(SessionKey.SESSION_USER);
		} else {
			logger.error(SESSION_NOT_EXIST);
			throw new SessionNotExist(SESSION_NOT_EXIST);
		}
	}

	public void addManipulatorSessionVariable(DataManipulator manipulator)
			throws SessionNotExist {
		if (session != null) {
			session.put(SessionKey.DATA_MANIPULATOR, manipulator);
		} else {
			logger.error(SESSION_NOT_EXIST);
			throw new SessionNotExist(SESSION_NOT_EXIST);
		}
	}

	public DataManipulator getManipulatorSessionVariable()
			throws SessionNotExist {

		if (session.containsKey(SessionKey.DATA_MANIPULATOR)) {
			return (DataManipulator) session.get(SessionKey.DATA_MANIPULATOR);
		} else {
			return null;
		}
	}

	public void removeManipulatorSessionVariable() throws SessionNotExist {
		if (session != null) {
			if (session.containsKey(SessionKey.DATA_MANIPULATOR)) {
				session.remove(SessionKey.DATA_MANIPULATOR);
			}
		} else {
			logger.error(SESSION_NOT_EXIST);
			throw new SessionNotExist(SESSION_NOT_EXIST);
		}
	}

	public boolean hasSearchParam() {
		try {
			DataManipulator manipulator = (DataManipulator) session.get(SessionKey.DATA_MANIPULATOR);
			if(manipulator.hasSearchParam()) {
				return true;
			}
			else {
				return false;
			}
		}
		catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	public void setSession(@SuppressWarnings("rawtypes") Map session) {
		this.session = session;
	}

	public STATUS getStatus() {
		return status;
	}

	public void setStatus(STATUS status) {
		this.status = status;
	}

	public boolean isLogin() {
		try {
			SystemUser user = (SystemUser) session.get(SessionKey.SESSION_USER);
			if (user != null) {
				return true;
			}
			return false;
		} catch (Exception e) {
			return false;
		}
	}

	public boolean isAdmin() {
		try {
			SystemUser user = (SystemUser) session.get(SessionKey.SESSION_USER);
			/*
			 * if(user != null && user.getUserRole() == User.ADMIN_ROLE){ return
			 * true; }
			 */
			return false;
		} catch (Exception e) {
			return false;
		}
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

}
